<?php

/**
 * Cuadros
 * 
 * @package Sgit 
 * @author Juan Manuel Martinez Carrillo
 * @copyright Juan Manuel Martinez Carrillo
 * @version 2012
 * @access public
 * @revision 17/02/2012 9:06:
 * 
 */



class CuadrosController extends Sgit_Controller_Action
{
    
    protected $_main_model = 'Cuadros';
    protected $_main_form = 'Cuadros';
    protected $_checkZona = TRUE;
    
    
    function _begin()
    {
        $this->_file_upload[1] = Sgit_Util_FileUpload::run('cuadros','informe');
        $this->_file_upload[2] = Sgit_Util_FileUpload::run('cuadros','unifilar');
    }
    
    
    
    
    public function indexAction()
    {
        

        $filtros = new Sgit_Form_Filtros();
        $salas =    new Sgit_Form_Element_Select_Salas('id_sala');
        $servicio = new Sgit_Form_Element_Select_Xml('ServicioCuadro');

        $cc=new Sgit_Form_Element_Select_CC('id_cc');
        $cc->setLabel('Origen suministro');
        
        $filtros->addFormSelect($salas)
        ->addFormSelect($servicio)
        ->addFormSelect($cc)
        ->addExcel()
        ->addpdf()
        ->myRender();
       

        $values = $this->_getFiltrosValues($filtros);

        
        $values['id_zona']=$this->_idZona;


        

        if ($this->_request->isPost()){
            $rows=$this->_model->default->getCuadros($values);
            
            
            foreach ($rows as & $v)
            {
               $v['proyectos']=$this->getProyectos($v['proyectos']);
            }
            $this->view->rows=$rows;
            
        }
            
        else 
            $this->view->msgPost='Usa los filtros para realizar la busqueda';
        
        $this->_pdfSubTitle = $this->zona;
        $this->_export($this->view->rows);
        
        $xml = $this->_getXml($this->_actionName);

    }
    
    function getProyectos($rows)
    {
        
        foreach($rows as $v)
        {
            if(is_numeric($v)) $pro[$v]++;
        }
        arsort($pro);
        
        return $pro;
    }
    
    
    
    
    public function insertarAction()
    {

        $this->_insertar();
    }

    public function editarAction()
    {

        $this->_editar();
    }

    public function borrarAction()
    {
        $this->_borrar();
    }
    
    
    protected function _prepareValuesToForm($row)
    {
        $row['fecha_entrega']=Sgit_Filter_DateDbToForm::filter($row['fecha_entrega']);
        return $row;
    }
    
    protected function _prepareValuesToDb()
    {
        $row = $this->_form->getValues();
        
        Sgit_Util_removeFiles::run($this->_file_upload[1]['tabla'],$this->_file_upload[1]['key'],$this->_id,$row[$this->_file_upload[1]['activo']]);
        unset($row[$this->_file_upload[1]['name']]);
        unset($row[$this->_file_upload[1]['activo']]);
        
        Sgit_Util_removeFiles::run($this->_file_upload[2]['tabla'],$this->_file_upload[2]['key'],$this->_id,$row[$this->_file_upload[2]['activo']]);
        unset($row[$this->_file_upload[2]['name']]);
        unset($row[$this->_file_upload[2]['activo']]);
        
      
        return $row;
    }


    protected function _getFila($id)
    {
        
        $row= $this->_model->default->getCuadros($id);
        $row['proyectos']=$this->getProyectos($row['proyectos']);
       

        return $row;
 
    }

    function checkdelineanteAction()
    {
        $checked = $this->_getParam('checked');
        $id      = $this->_getParam('id');
        $name    = $this->_getParam('name');
        $data[$name]=($checked=='true') ? 1 : 0;
        
        $json['status']=0; 
        if($name=='validado_delineante'){
            $data['fecha_validado_delineante']=($checked=='true') ? date('Y-m-d') : null;
            if($this->_model->default->updateRow($data,$id))$json['status']=1;
        }
        $this->_helper->json($json);
    }
    
    function checkfacilityAction()
    {
        $checked = $this->_getParam('checked');
        $id      = $this->_getParam('id');
        $name    = $this->_getParam('name');
        $data[$name]=($checked=='true') ? 1 : 0;
        
        $json['status']=0; 
        if($name=='validado_facility'){
            $data['fecha_validado_facility']=($checked=='true') ? date('Y-m-d') : null;
            if($this->_model->default->updateRow($data,$id))$json['status']=1;
        }
        $this->_helper->json($json);
    }
    
    function checkdirectorAction()
    {
        $checked = $this->_getParam('checked');
        $id      = $this->_getParam('id');
        $name    = $this->_getParam('name');
        $data[$name]=($checked=='true') ? 1 : 0;
        
        
        $json['status']=0; 
        if($name=='validado_director'){
            $data['fecha_validado_director']=($checked=='true') ? date('Y-m-d') : null;
            if($this->_model->default->updateRow($data,$id))$json['status']=1;
        }
        $this->_helper->json($json);
    }



}
